package com.foreveross.proxyip.web.action.monitor;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.foreveross.proxyip.core.BaseCode;
import com.foreveross.proxyip.core.http.ipdoload.IpDoloadManager;
import com.foreveross.proxyip.core.pools.ChannelPool;
import com.foreveross.proxyip.core.pools.PoolsManager;
import com.foreveross.proxyip.core.redis.RedisExecutor;
import com.foreveross.proxyip.core.zk.ZkAppCenter;
import com.foreveross.proxyip.exception.ProxyIpServiceException;
import com.foreveross.proxyip.monitor.vo.MonitorVO;
import com.foreveross.proxyip.web.action.BaseAction;

@Namespace("/monitor")
@ParentPackage("json-default")
public class MonitorAction extends BaseAction  {
	MonitorVO vo = new MonitorVO();
	@Autowired
	IpDoloadManager ipDoloadManager; 
	
	@Autowired
	private PoolsManager poolsManager;
	@Action(value="toJsp",results={@Result(name="dispatcher", type="dispatcher",location="/monitor/monitorCenter.jsp")})
	public String toJsp()throws Exception{
		vo.setAppNames(ZkAppCenter.getInstance().getZK().getChildren(ZkAppCenter.getInstance().appGroup, false));
//		ZkAppCenter.getInstance().getZK().getChildren(path, watch)
		vo.setTaskList(ZkAppCenter.getInstance().getZK().getChildren(ZkAppCenter.getInstance().tasksPath, false));
		return "dispatcher";
	}
	
	@Action(value="reRegisterZK", results={@Result(name="JSON", type="json", params={"root","vo.callJsonResult"})})
	public String reRegisterZK() throws Exception {
		try{
			ZkAppCenter.getInstance().reRegister();
			vo.setCallJsonResult("操作成功。");
		}catch(Exception e){
			e.printStackTrace();
			vo.setCallJsonResult("操作失败。");
		}
		return "JSON";
	}
	
	@Action(value="redisFlushAll", results={@Result(name="JSON", type="json", params={"root","vo.callJsonResult"})})
	public String redisFlushAll() throws Exception{
		try{
		new RedisExecutor<Boolean>() {
			@Override
			public Boolean run() throws Exception {
				this.jedis.flushAll();
				return null;
			}
		}.exec();
		vo.setCallJsonResult("操作成功。");
		}catch(Exception e){
			e.printStackTrace();
			vo.setCallJsonResult("操作失败");
		}
		
		return "JSON";
	}
	
	@Action(value="reloadWebSite", results={@Result(name="JSON", type="json", params={"root","vo.callJsonResult"})})
	public String reloadIpWebsites() throws Exception{
		try{
		ipDoloadManager.reloadIpWebsites();
		vo.setCallJsonResult("操作成功。");
		}catch(Exception e){
			e.printStackTrace();
			vo.setCallJsonResult("操作失败");
		}
		return "JSON";
	}
	@Action(value="reloadBaseCode", results={@Result(name="JSON", type="json", params={"root","vo.callJsonResult"})})
	public String reloadBaseCode() throws Exception{
		try{
		BaseCode.getInstance().initBaseCode();
		ZkAppCenter.getInstance().doNormalTask("reloadBaseCode");
		vo.setCallJsonResult("操作成功。");
		}catch(Exception e){
			e.printStackTrace();
			vo.setCallJsonResult("操作失败。");
		}
		return "JSON";
	}
	

	public MonitorVO getVo() {
		return vo;
	}

	public void setVo(MonitorVO vo) {
		this.vo = vo;
	}
	
}
